import { observable, action } from 'mobx';
import { message, Modal } from 'antd';
import window from "../../util";
import _ from 'lodash';
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class CustomDialogStore {
    @observable visible: boolean = false;
    @observable content: string = '';
    @observable title: string = '';
    @observable loading: boolean = false;
    @observable okText: string = "";
    onOkFuncs: string = '';

    @observable tempParams: any = {};
    @observable typeName: string = "";
    @observable height:string = '200';
    @observable width:string = '200';

    constructor() {
        this.okText = getLabel(382958, "确定");
    }

    @action
    onCancel = () => {
        this.visible = false;
        this.tempParams = {};
        this.typeName = "";
    }
    @action
    onOk = () => {
        if (this.onOkFuncs) {
            const run = () => {
                eval(this.onOkFuncs);
            }
            run.bind(this)();
        }
        this.onCancel()
    }

    @action
    setCustomDialog = (customDialog: any) => {
        _.each(customDialog, (v, k) => {
            _.set(this, k, v);
        })
    }

    @action
    showCustomDialog = (APIFunc: Function, APIParams: any, name: string = "", ...props) => {
        this.tempParams = APIParams;
        this.typeName = name;
        APIFunc(APIParams).then(action(({ status, error, errorCode, showError, message: msg, customDialog, onOk,height,width }) => {
            if (status == '1') {
                if (showError) {
                    Modal.error({ content: msg, align: 'top', className: "cube-custom-dialog-error" });
                } else {
                    this.setCustomDialog({ ...props, ...customDialog});
                    this.onOkFuncs = `this.${onOk}()`;
                    this.height = height&&Number(height) < 200 ? '300': Number(height)+100+'';
                }
            } else {
                message.error(`[${getLabel(127353, "错误") + errorCode}]:${error}`);
            }
        }))
    }

    toPrint = () => {
        if (this.typeName == "qrCode") {
            //二维码
            window.modeopenFullWindowHaveBar(`/spa/cube/index.html#/main/cube/qrCode?modeId=${this.tempParams.modeId}&customId=${this.tempParams.customId}&formId=${this.tempParams.formId}&billId=${this.tempParams.billid}&from=card`);
        } else if (this.typeName == "barCode") {
            //条形码
            window.modeopenFullWindowHaveBar(`/spa/cube/index.html#/main/cube/barCode?modeId=${this.tempParams.modeId}&customId=${this.tempParams.customId}&formId=${this.tempParams.formId}&billId=${this.tempParams.billid}&from=card`);
        }

    }
}